package cn.showcon.firstapp.service;

import ch.qos.logback.classic.Level;
import dev.langchain4j.internal.Json;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.qianfan.QianfanChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.V;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

import java.time.LocalDate;

/**
 * @Author Xue Lanbin
 */
@Slf4j
public class LlmService2 {

    public static ChatLanguageModel createChatLanguageModel() {
        QianfanChatModel qianfan = QianfanChatModel.builder()
                .modelName("ERNIE-4.0-8K")
                .endpoint("completions_pro")
                .apiKey("jbug222LxcOwvHiWLuCOmjd2")
                .secretKey("hktVnuX6XR7Cpeja0UClboehd2h8kEYp")
                .responseFormat("json_object")
                .logRequests(true)
                .logResponses(true)
                .build();
        return qianfan;
    }

    public static class Example1 {

        interface Assistant {

            @SystemMessage("You are a good friend of mine. Answer using slang.")
            String chat(String userMessage);
        }

        interface Friend {

            @UserMessage("You are a good friend of mine. Answer using slang. {{message}}")
            String chat(@V("message") String userMessage);
        }

        public static void main(String[] args) {
            ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory
                    .getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
            root.setLevel(Level.DEBUG);

            System.out.println(log.isDebugEnabled());

            ChatLanguageModel model = createChatLanguageModel();

            Assistant assistant = AiServices.create(Assistant.class, model);
            String answer = assistant.chat("Hello");
            System.out.println(answer);
            System.out.println("-----");

            Friend friend = AiServices.create(Friend.class, model);
            String answer2 = friend.chat("Hello");
            System.out.println("-----");
            System.out.println(answer2);
        }
    }

    public static class Example2 {

        class Person {
            String firstName;
            String lastName;
            Address address;
        }

        class Address {
            String street;
            Integer streetNumber;
            String city;
        }

        interface PersonExtractor {

            @UserMessage("Extract information about a person from {{it}}")
            Person extractPersonFrom(String text);
        }

        public static void main(String[] args) {
            ChatLanguageModel model = createChatLanguageModel();

            PersonExtractor personExtractor = AiServices.create(PersonExtractor.class, model);
            String text = """
                    In 1968, amidst the fading echoes of Independence Day,
                    a child named John arrived under the calm evening sky.
                    This newborn, bearing the surname Doe, marked the start of a new journey.
                    He was welcomed into the world at 345 Whispering Pines Avenue
                    a quaint street nestled in the heart of Springfield
                    an abode that echoed with the gentle hum of suburban dreams and aspirations.
                    """;

            Person person = personExtractor.extractPersonFrom(text);
            System.out.println(person);
            System.out.println(person.lastName + ", " + person.firstName);
            System.out.println(person.address.city);
        }
    }
}
